package tr.muyat.accelerometer.sender;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * 
 * @author fxulusoy
 */
public class SenderClient {
	
	private static final int PORT = 5555;
	private static final String SERVER_IP = "192.168.137.1";
	
	private Socket socket;
	private OutputStream outputStream;
	
	/**
	 * Constructor
	 */
	public SenderClient() {
		
	}
	
	public boolean startConnection() {
        try {
            // Create a connection to the server socket
            // InetAddress host = InetAddress.getLocalHost();
            // Socket socket = new Socket(host.getHostName(), port);
            socket = new Socket(SERVER_IP, PORT);
            
            outputStream = socket.getOutputStream();
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        
		return true;
    }
	
	public void stopConnection() {
		try {
			outputStream.close();
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public boolean send(byte[] buffer) {
		if (outputStream == null) {
			return false;
		}
		
		try {
			outputStream.write(buffer);
			outputStream.flush();
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		} 
		
		return true;
	}
	
	
}

